﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class System_MasterPage3 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Account"] != null)
        {
            // this.labLogon.Text = "";
            this.labLogon.Visible = false;
            lab1.Visible = false;

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            HZ.MBSM.DAL.AccountType _AccountType = (HZ.MBSM.DAL.AccountType)SysFun.ToInt(_Account.Type);

            string strPageDescribePath = Server.MapPath("../System/PageDescribe.xml");
            HZ.MBSM.BLL.PageDescribeCollection _PageDescribeCollection = new HZ.MBSM.BLL.PageDescribeCollection();
            _PageDescribeCollection.LoadXML(strPageDescribePath);

            foreach (HZ.MBSM.BLL.PageDescribe _PageDescribe in _PageDescribeCollection.ChildrenPageDescribe)
            {
                for (int intIndex = _PageDescribe.ChildrenPageDescribe.Count - 1; intIndex >= 0; intIndex--)
                {
                    bool bolIsChecked = false;

                    foreach (HZ.MBSM.DAL.AccountType _AccountTypePage in _PageDescribe.ChildrenPageDescribe[intIndex].UseAccountType)
                    {
                        if (_AccountTypePage == _AccountType)
                        {
                            bolIsChecked = true;
                            break;
                        }
                    }

                    if (bolIsChecked == false)
                    {
                        _PageDescribe.ChildrenPageDescribe.RemoveAt(intIndex);
                    }
                }
            }

         
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (_Account.Type == "4")
            {
                sb.Append("<table width='200' border='0' cellspacing='0' cellpadding='0'>");
                sb.Append("<tr><td><img src='../Images/2_left_title1.jpg' width='200' height='14'></td></tr>");
                sb.Append("<tr><td align='center' background='../Images/left_bg.jpg'>");
                sb.Append("<table width='200' border='0' cellspacing='4' cellpadding='0'>");
                sb.Append("<tr><td>");
                sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                //sb.Append("<tr>");
                //sb.Append("<td><img src='../Images/logo_JD.gif' with='200'/></td>");
                //sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'><strong><span style=‘font-family: 宋体'>欢迎 <font color='#0066CC'>" + _Account.DWName + " </font> 登录系统</span></strong></td>");
                sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'><strong><span style=‘font-family: 宋体'>用户名:<font color='#0066CC'>" + _Account.AccountName + "</font> </span></strong></td>");
                sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'><strong><span style=‘font-family: 宋体'>注册号:<font color='#0066CC'>" + _Account.UserCode + "</font></span></strong> </td>");
                sb.Append("</tr>");
                sb.Append("<tr><td class='2_left_bg2'><img src='../Images/spacer.gif' width='40' height='10'>");
                sb.Append("<a href='../HomePage/Logout.aspx'><strong><span style=‘font-family: 宋体'>◇注销</a>");
                sb.Append("</td></tr>");

                sb.Append("</table>");
                sb.Append("</td></tr>");

                foreach (HZ.MBSM.BLL.PageDescribe _PageDescribe in _PageDescribeCollection.ChildrenPageDescribe)
                {
                    if (_PageDescribe.ChildrenPageDescribe.Count > 0)
                    {
                        sb.Append("<tr><td>");
                        sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                        sb.Append("<tr>");
                        sb.Append("<td class='2_left_bg' style='behavior:url(../System/ItemSortPucker.htc)'><img src='../Images/spacer.gif' width='40' height='10'><strong><span style=‘font-family: 宋体'>" + _PageDescribe.Name + "</span></strong></td>");
                        sb.Append("</tr>");
                        sb.Append("<tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'>");

                        foreach (HZ.MBSM.BLL.PageDescribe _PageDescribeChildren in _PageDescribe.ChildrenPageDescribe)
                        {
                            sb.Append("<tr><td class='2_left_bg2'><img src='../Images/spacer.gif' width='40' height='10'>");
                            sb.Append("<a href='" + _PageDescribeChildren.Url + "'><strong><span style=‘font-family: 宋体'>◇" + _PageDescribeChildren.Name + "</span></strong></a>");
                            sb.Append("</td></tr>");
                        }

                        sb.Append("</table></td></tr>");
                        sb.Append("</table>");
                        sb.Append("</td></tr>");
                    }
                }

                sb.Append("</table>");
                sb.Append("</td></tr><tr><td><img src='../Images/left_bottom.jpg' width='200' height='14'></td></tr>");
                sb.Append("</table>");
            }
            else
            {
                sb.Append("<table width='200' border='0' cellspacing='0' cellpadding='0'>");
                sb.Append("<tr><td><img src='../Images/2_left_title1.jpg' width='200' height='14'></td></tr>");
                sb.Append("<tr><td align='center' background='../Images/left_bg.jpg'>");
                sb.Append("<table width='200' border='0' cellspacing='4' cellpadding='0'>");
                sb.Append("<tr><td>");
                sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'>欢迎 <font color='#0066CC'>" + _Account.DWName + " </font> 登录系统</td>");
                sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'>用户名:<font color='#0066CC'>" + _Account.AccountName + "</font> </td>");
                sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'>注册号:<font color='#0066CC'>" + _Account.UserCode + "</font> </td>");
                sb.Append("</tr>");
                sb.Append("<tr><td class='2_left_bg2'><img src='../Images/spacer.gif' width='40' height='10'>");
                sb.Append("<a href='../HomePage/Logout.aspx'>◇注销</a>");
                sb.Append("</td></tr>");

                sb.Append("</table>");
                sb.Append("</td></tr>");

                foreach (HZ.MBSM.BLL.PageDescribe _PageDescribe in _PageDescribeCollection.ChildrenPageDescribe)
                {
                    if (_PageDescribe.ChildrenPageDescribe.Count > 0)
                    {
                        sb.Append("<tr><td>");
                        sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                        sb.Append("<tr>");
                        sb.Append("<td class='2_left_bg' style='behavior:url(../System/ItemSortPucker.htc)'><img src='../Images/spacer.gif' width='40' height='10'>" + _PageDescribe.Name + "</td>");
                        sb.Append("</tr>");
                        sb.Append("<tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'>");

                        foreach (HZ.MBSM.BLL.PageDescribe _PageDescribeChildren in _PageDescribe.ChildrenPageDescribe)
                        {
                            sb.Append("<tr><td class='2_left_bg2'><img src='../Images/spacer.gif' width='40' height='10'>");
                            sb.Append("<a href='" + _PageDescribeChildren.Url + "'>◇" + _PageDescribeChildren.Name + "</a>");
                            sb.Append("</td></tr>");
                        }

                        sb.Append("</table></td></tr>");
                        sb.Append("</table>");
                        sb.Append("</td></tr>");
                    }
                }

                sb.Append("</table>");
                sb.Append("</td></tr><tr><td><img src='../Images/left_bottom.jpg' width='200' height='14'></td></tr>");
                sb.Append("</table>");
            }
            labPage.Text = sb.ToString();

            //多层树节点

            //Label labNodes = new Label();
            //ReadPageDescribeNode(_PageDescribeCollection.ChildrenPageDescribe, labNodes, 0);
            //labPage.Controls.Add(labNodes);
        }
        else
        {
            lab1.Text = WriteBoard();
            lab1.Visible = true;
        }
    }

    //将PageDescribe树写入到 ListView 中

    private void ReadPageDescribeNode(System.Collections.Generic.List<HZ.MBSM.BLL.PageDescribe> pageDescribes, Label treeNodeCollection, int space)
    {
        foreach (HZ.MBSM.BLL.PageDescribe _PageDescribe in pageDescribes)
        {
            Label labNode = new Label();
            string strSpace = "";
            for (int intIndex = 0; intIndex < space; intIndex++)
            {
                strSpace += "　";
            }
            if (_PageDescribe.Url != null && _PageDescribe.Url.Length > 0)
            {
                labNode.Text = strSpace + "<a href='" + _PageDescribe.Url + "'>" + _PageDescribe.Name + "</a><br>";
            }
            else
            {
                labNode.Text = strSpace + _PageDescribe.Name + "<br>";
            }
            treeNodeCollection.Controls.Add(labNode);

            Label labChildren = new Label();
            treeNodeCollection.Controls.Add(labChildren);

            if (_PageDescribe.ChildrenPageDescribe.Count > 0)
            {
                ReadPageDescribeNode(_PageDescribe.ChildrenPageDescribe, labChildren, space + 1);
            }
        }
    }
    public string Display_Title(object Title, int Len)
    {
        string Title_str = Title.ToString().Trim();
        if (Title_str != "")
        {
            //if(Title_str.Length > Len)
            //	Title_str = Title_str.Substring(0,Len)+"..";
            int ShowNum = WordCount(Title_str, Len);
            if (Title.ToString().Length > ShowNum)
                Title_str = Title.ToString().Substring(0, ShowNum - 1) + "....";
        }
        else
            Title_str = "";

        return Title_str;
    }
    public int WordCount(string Operand, int MaxNum)
    {
        IEnumerator OperandEnum = Operand.GetEnumerator();
        char ch = ' ';
        int i = 0;
        for (int CharCount = 0; OperandEnum.MoveNext() && CharCount <= MaxNum; i++)
        {
            ch = Convert.ToChar(OperandEnum.Current);
            if ((ch >= 32 && ch <= 126) || ch == '=')
                CharCount++;
            else
                CharCount += 2;
        }
        return i;
    }
    //问题反馈
    public string WriteBoard()
    {
        int i = 0;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 9 id,title from board  order by  id desc ";
        System.Data.IDataReader dr = db.DataReader(strSql);



        sb.Append("<table width='200' border='0' cellspacing='0' cellpadding='0'><tr><td><img src='../Images/qst.jpg' width='200' height='30'></td></tr><tr><td background='../Images/leftbg.jpg'><table width='90%' border='0'  align='center' cellpadding='4' cellspacing='1'><tr><td >");


        sb.Append("<table  width='96%'  border=0 align=center cellpadding=6 cellspacing=1>");
        while (dr.Read())
        {
            i++;
            sb.Append("<tr ><td class='dotbottomline' >");
            sb.Append("<img src='../images/ico7.gif' width='8' height='5' hspace='4' vspace='1'>");
            sb.Append("<a href='../HomePage/Boardinfo.aspx?Id=" + dr["id"].ToString() + "' target=_blank>" + Display_Title(dr["Title"].ToString(), 18) + "</a>");
            sb.Append("</td></tr>");
        }
        for (int j = i; j < 9; j++)
            sb.Append("<tr><td class='dotbottomline'>&nbsp;</td></tr>");

        sb.Append("</table>");
        dr.Close();
        dr.Dispose();
        db.Close();
        sb.Append("</td></tr><tr align=right ><td><a href='../HomePage/BordList.aspx'>更多...</a></td></tr></table></td></tr><tr align=right><td></td></tr></table>");
        return sb.ToString();
    }
    //操作帮助
    public string Writehelp()
    {
        int i = 0;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 9 Id,Title from HelpTable  order by  id desc ";
        System.Data.IDataReader dr = db.DataReader(strSql);


       // <img src='../Images/help.jpg' width='200' height='30'/>
        sb.Append("<table width='200' border='0' cellspacing='0' cellpadding='0'><tr><td></td></tr><tr><td background='../Images/leftbg.jpg'><table width='90%' border='0'  align='center' cellpadding='4' cellspacing='1'><tr><td >");


        sb.Append("<table  width='96%'  border=0 align=center cellpadding=6 cellspacing=1>");
        while (dr.Read())
        {
            i++;
            sb.Append("<tr ><td class='dotbottomline' >");
            sb.Append("<img src='../images/ico7.gif' width='8' height='5' hspace='4' vspace='1'>");
            sb.Append("<a href='../HomePage/HelpInfo.aspx?Id=" + dr["id"].ToString() + "' target=_blank>" + Display_Title(dr["Title"].ToString(), 18) + "</a>");
            sb.Append("</td></tr>");
        }
        for (int j = i; j < 9; j++)
            sb.Append("<tr><td class='dotbottomline'>&nbsp;</td></tr>");

        sb.Append("</table>");
        dr.Close();
        dr.Dispose();
        db.Close();
        sb.Append("</td></tr><tr align=right ><td><a href='../HomePage/HelpList.aspx'>更多...</a></td></tr></table></td></tr><tr align=right><td></td></tr></table>");
        return sb.ToString();
    }
    //友情链接 
    //public string Writelink()
    //{

    //    int i = 0;
    //    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    //    HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

    //    string strSql = "select top 6 LinkId,Title,DownLoad,LinkWWW from link where IsShow=0 order by DownLoad desc ";
    //    System.Data.IDataReader dr = db.DataReader(strSql);

    //    sb.Append("<table  width='96%' border=0 align=center cellpadding=6 cellspacing=1>");

    //    while (dr.Read())
    //    {
    //        i++;
    //        sb.Append("<tr><td align='center'>");

    //        if (dr["DownLoad"].ToString().Trim() != "")
    //        {
    //            sb.Append("<a href='" + dr["LinkWWW"].ToString() + "' target=_blank><img style='border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none' src='../uploadfile/" + dr["DownLoad"].ToString().Trim() + "' width='154' height='40'></a>");
    //        }
    //        else
    //        {
    //            sb.Append("<a href='" + dr["LinkWWW"].ToString() + "' target=_blank>" + dr["Title"].ToString().Remove(2) + "</a> ");
    //        }
    //        sb.Append("</td></tr>");
    //    }
    //    for (int j = i; j < 6; j++)
    //        sb.Append("<tr><td class='dotbottomline'>&nbsp;</td></tr>");
    //    sb.Append("</table>");
    //    dr.Close();
    //    dr.Dispose();
    //    db.Close();

    //    return sb.ToString();
    //}
}
